package com.team2.insulatingtools.modular.tools.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import com.team2.insulatingtools.modular.tools.entity.Category;
import com.team2.insulatingtools.modular.tools.enums.CategoryExceptionEnum;
import com.team2.insulatingtools.modular.tools.mapper.CategoryMapper;
import com.team2.insulatingtools.modular.tools.pojo.request.CategoryRequest;
import com.team2.insulatingtools.modular.tools.service.CategoryService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务实现层
 *
 * @author team2
 * @date 2023/04/07 16:03
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

	@Override
    public void add(CategoryRequest categoryRequest) {
        Category category = new Category();
        BeanUtil.copyProperties(categoryRequest, category);
        this.save(category);
    }

    @Override
    public void del(CategoryRequest categoryRequest) {
        Category category = this.queryCategory(categoryRequest);
        this.removeById(category.getId());
    }

    @Override
    public void edit(CategoryRequest categoryRequest) {
        Category category = this.queryCategory(categoryRequest);
        BeanUtil.copyProperties(categoryRequest, category);
        this.updateById(category);
    }

    @Override
    public Category detail(CategoryRequest categoryRequest) {
        return this.queryCategory(categoryRequest);
    }

    @Override
    public PageResult<Category> findPage(CategoryRequest categoryRequest) {
        LambdaQueryWrapper<Category> wrapper = createWrapper(categoryRequest);
        Page<Category> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<Category> findList(CategoryRequest categoryRequest) {
        LambdaQueryWrapper<Category> wrapper = this.createWrapper(categoryRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author team2
     * @date 2023/04/07 16:03
     */
    private Category queryCategory(CategoryRequest categoryRequest) {
        Category category = this.getById(categoryRequest.getId());
        if (ObjectUtil.isEmpty(category)) {
            throw new ServiceException(CategoryExceptionEnum.CATEGORY_NOT_EXISTED);
        }
        return category;
    }

    /**
     * 创建查询wrapper
     *
     * @author team2
     * @date 2023/04/07 16:03
     */
    private LambdaQueryWrapper<Category> createWrapper(CategoryRequest categoryRequest) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();

        Integer id = categoryRequest.getId();
        String name = categoryRequest.getName();
        Float workingVoltage = categoryRequest.getWorkingVoltage();
        Float testingVoltage = categoryRequest.getTestingVoltage();
        Integer length = categoryRequest.getLength();
        Integer testDuration = categoryRequest.getTestDuration();
        Integer testPeriod = categoryRequest.getTestPeriod();
        String unit = categoryRequest.getUnit();
        String useage = categoryRequest.getUseage();
        String tips = categoryRequest.getTips();
        Integer speciality = categoryRequest.getSpeciality();

        queryWrapper.eq(ObjectUtil.isNotNull(id), Category::getId, id);
        queryWrapper.like(ObjectUtil.isNotEmpty(name), Category::getName, name);
        queryWrapper.eq(ObjectUtil.isNotNull(workingVoltage), Category::getWorkingVoltage, workingVoltage);
        queryWrapper.eq(ObjectUtil.isNotNull(testingVoltage), Category::getTestingVoltage, testingVoltage);
        queryWrapper.eq(ObjectUtil.isNotNull(length), Category::getLength, length);
        queryWrapper.eq(ObjectUtil.isNotNull(testDuration), Category::getTestDuration, testDuration);
        queryWrapper.eq(ObjectUtil.isNotNull(testPeriod), Category::getTestPeriod, testPeriod);
        queryWrapper.like(ObjectUtil.isNotEmpty(unit), Category::getUnit, unit);
        queryWrapper.like(ObjectUtil.isNotEmpty(useage), Category::getUseage, useage);
        queryWrapper.like(ObjectUtil.isNotEmpty(tips), Category::getTips, tips);
        queryWrapper.eq(ObjectUtil.isNotNull(speciality), Category::getSpeciality, speciality);

        return queryWrapper;
    }

}
